<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    // const list =
    //   [
    //     { "id": "1175311267684352000", "pid": "", "name": "市场部" },
    //     { "id": "1175311325720936448", "pid": "", "name": "财务部" },
    //     { "id": "1175311373083017216", "pid": "", "name": "技术部" },
    //     { "id": "1235398917619130368", "pid": "1175311267684352000", "name": "南京事业部", },
    //     { "id": "1235399011458293760", "pid": "1175311267684352000", "name": "北京事业部", },
    //     { "id": "1235395178363559936", "pid": "1175311325720936448", "name": "财务核算部", },
    //     { "id": "1235398264104624128", "pid": "1175311325720936448", "name": "税务管理部", },
    //     { "id": "1235398608847052800", "pid": "1175311373083017216", "name": "Java研发部" },
    //     { "id": "1235398661355544576", "pid": "1175311373083017216", "name": "PHP研发部" },
    //   ]
    const list =
      [
        { "id": "1175311267684352000", "pid": "", "name": "市场部" }, 
        { "id": "1235398917619130368", "pid": "1175311267684352000", "name": "南京事业部", },
        { "id": "1235399011458293760", "pid": "1175311267684352000", "name": "北京事业部", },
      ]
    const result = transListToTreeData(list, '')
    console.log(result)


    function transListToTreeData(list, searchVal) {
      // 先找一级，根据一级找二级
      const arr = []
      list.forEach(item => {
        if (item.pid === searchVal) {
          // 根据一级找二级
          const children = transListToTreeData(list, item.id)
          item.children = children
          arr.push(item)
        }
      })
      return arr
    }
  </script>
</body>

</html>